package com.woniu.adapter;

import com.woniu.inlet.web.fo.FixedInputBuyFo;
import com.woniu.inlet.web.vo.FixedMsgVo;
import com.woniu.outlet.mysql.dao.PersonalFixedDao;
import com.woniu.outlet.mysql.po.DailyCodeValuationMySQL;
import com.woniu.outlet.mysql.po.PersonalFixedMySQL;
import com.woniu.outlet.mysql.po.PersonalFixedMySQLExample;
import com.woniu.outlet.redis.CodeOneRedisDao;
import com.woniu.outlet.redis.po.DailyCodeTOne;
import com.woniu.service.domain.FundOrGoldBuyDto;
import com.woniu.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class PersonalFixedAdapter {

    @Autowired
    private PersonalFixedDao dao;

    @Autowired
    private CodeOneRedisDao redisDao;

    public void insert0(FixedInputBuyFo dto, String fixedType, String fixedDay) {
        PersonalFixedMySQL mySQL = PersonalFixedMySQL.builder()
                .userId(dto.getUserId())
                .productType("fund")
                .codeId(dto.getCodeId())
                .fixedAmount(dto.getBuyAmount())
                .fixedType(fixedType)
                .fixedDay(Integer.parseInt(fixedDay))
                .createDate(DateUtil.getSendDate())
                .status(0)
                .build();
        dao.insert(mySQL);
    }

    public void insert_1(FixedInputBuyFo dto, String fixedType, String fixedDay) {
        PersonalFixedMySQL mySQL = PersonalFixedMySQL.builder()
                .userId(dto.getUserId())
                .productType("fund")
                .codeId(dto.getCodeId())
                .fixedAmount(dto.getBuyAmount())
                .fixedType(fixedType)
                .fixedDay(Integer.parseInt(fixedDay))
                .createDate(DateUtil.getSendDate())
                .status(-1)
                .build();
        dao.insert(mySQL);
    }

    /**
     * @param day  今天几号
     * @param week 今天周几
     */
    public List<PersonalFixedMySQL> findMeetMonthOrWeekFixedDate(int day, int week, int status) {
        PersonalFixedMySQLExample example = new PersonalFixedMySQLExample();
        example.or()
                .andFixedTypeEqualTo("month")
                .andFixedDayEqualTo(day)
                .andStatusEqualTo(status);
        example.or()
                .andFixedTypeEqualTo("week")
                .andFixedDayEqualTo(week)
                .andStatusEqualTo(status);
        return dao.selectByExample(example);
    }

    /**
     * 得到用户的定投单子
     *
     * @param id
     */
    public FixedMsgVo getFixedMsgById(Long id,int buyId) {
        PersonalFixedMySQL mySQL = dao.selectByPrimaryKey(id);
        FixedMsgVo vo = new FixedMsgVo();
        BeanUtils.copyProperties(mySQL, vo);
        DailyCodeTOne tOne = redisDao.findById(vo.getCodeId()).orElseThrow(NullPointerException::new);
        vo.setName(tOne.getName());
        vo.setId(Long.valueOf(buyId));
        return vo;
    }


    public void updateStatus(String date) {
        PersonalFixedMySQLExample example = new PersonalFixedMySQLExample();
        PersonalFixedMySQLExample.Criteria criteria = example.createCriteria();
        criteria.andCreateDateEqualTo(date);
        criteria.andStatusEqualTo(-1);
        example.or()
                .andStatusEqualTo(-2);
        List<PersonalFixedMySQL> lists = dao.selectByExample(example);
        for (PersonalFixedMySQL p : lists) {
            p.setStatus(0);
            dao.updateByPrimaryKey(p);
        }
        log.debug("定投当日的单子有{}条", lists.size());
    }

    public void updateStatus_2(Long fixedId) {
        PersonalFixedMySQL mySQL = dao.selectByPrimaryKey(fixedId);
        mySQL.setStatus(-2);
        dao.updateByPrimaryKey(mySQL);
    }
}
